# -*- coding: UTF-8 -*-
#
# config.py
"""
       Copyright 2009-2014 Giuseppe Penone <giuspen@gmail.com>

       This program is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published by
       the Free Software Foundation; either version 3 of the License, or
       (at your option) any later version.

       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       GNU General Public License for more details.

       You should have received a copy of the GNU General Public License
       along with this program; if not, write to the Free Software
       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
       MA 02110-1301, USA.
"""
# This file has settings and gui for the prefs settings windows
#python standard modules
import os
import locale
import base64

#python 3 imports
import configparser  # python 3
from gi.repository import Gtk as gtk  # gtk3 module

#cherrytree modules
#from editor.spell_checking import pgsc_spellcheck
import cherrytree.cons as const
import cherrytree.debug.debug_stuff as debug_stuff  # debug module
#import gui.tree_functions

if const.HAS_APPINDICATOR:
    #from gi.repository import AppIndicator3 as appindicator
    import appindicator

SPELL_CHECK_LANG_DEFAULT = locale.getdefaultlocale()[0]

#ICONS_SIZE = {1: gtk.ICON_SIZE_MENU, 2: gtk.ICON_SIZE_SMALL_TOOLBAR, 3: gtk.ICON_SIZE_LARGE_TOOLBAR,
#              4: gtk.ICON_SIZE_DND, 5: gtk.ICON_SIZE_DIALOG}


class ConfigSettings(object):
    """
    config settings object - load settings fromm the config filename if no file use the defaults below.
    """
    def __init__(self, parent_inst):
        #default values if no config file found with the config_file_load() method
        self.parent = parent_inst  # object refenence of the parant object created this object
        self.node_path = None
        self.curr_colors = {'f': None, 'b': None}  # default colours foreground and background
        self.syntax_highlighting = const.RICH_TEXT_ID
        self.auto_syn_highl = "sh"
        self.style_scheme = const.STYLE_SCHEME_DEFAULT
        #font settings
        self.tree_font = "Sans 8"  # default tree font
        self.text_font = "Sans 9"  # default text font
        self.code_font = "Monospace 9"  # default code font
        self.rt_def_fg = const.RICH_TEXT_DARK_FG
        self.rt_def_bg = const.RICH_TEXT_DARK_BG
        self.tt_def_fg = const.TREE_TEXT_LIGHT_FG
        self.tt_def_bg = const.TREE_TEXT_LIGHT_BG
        self.palette_list = const.COLOR_PALETTE_DEFAULT
        self.col_link_webs = const.COLOR_48_LINK_WEBS
        self.col_link_node = const.COLOR_48_LINK_NODE
        self.col_link_file = const.COLOR_48_LINK_FILE  # default colour for link file
        self.col_link_fold = const.COLOR_48_LINK_FOLD  # defualt colour
        self.h_rule = const.HORIZONTAL_RULE
        self.special_chars = const.SPECIAL_CHARS_DEFAULT, const.STR_UTF8, const.STR_IGNORE

        # editor settings
        self.show_line_numbers = False
        self.spaces_instead_tabs = True
        self.cursor_position = 0
        self.tabs_width = 4
        self.anchor_size = 16
        self.embfile_size = 48
        self.line_wrapping = True
        self.wrapping_indent = -14
        self.auto_indent = True
        self.limit_undoable_steps = 20
        self.cherry_wrap_width = 130
        self.show_white_spaces = True
        self.highl_curr_line = True
        # window settings
        self.win_position = [10, 10]
        self.win_size = [600, 600]
        self.win_is_maximized = False
        self.systray = False
        self.tree_right_side = False
        self.start_on_systray = False
        # toolbar settings
        self.toolbar_icon_size = 1
        self.toolbar_visible = True
        #
        self.rest_exp_coll = 0
        self.expanded_collapsed_string = ""
        self.expcollnam1 = ""
        self.expcollnam2 = ""
        self.expcollnam3 = ""
        self.expcollstr1 = ""
        self.expcollstr2 = ""
        self.expcollstr3 = ""
        self.expcollsel1 = ""
        self.expcollsel2 = ""
        self.expcollsel3 = ""
        self.expcollcur1 = ""
        self.expcollcur2 = ""
        self.expcollcur3 = ""
        self.pick_dir = ""
        self.link_type = const.LINK_TYPE_WEBS
        # table settings
        self.table_rows = 3
        self.table_columns = 3
        self.table_column_mode = "rename"
        self.table_col_min = 40
        self.table_col_max = 60
        #
        self.use_appind = False
        # link settings
        self.weblink_custom_action = [False, const.LINK_CUSTOM_ACTION_DEFAULT_WEB]
        self.filelink_custom_action = [False, const.LINK_CUSTOM_ACTION_DEFAULT_FILE]
        self.folderlink_custom_action = [False, const.LINK_CUSTOM_ACTION_DEFAULT_FILE]
        # time settings
        self.timestamp_format = "%Y/%m/%d - %H:%M"
        self.enable_mod_time_sentinel = True
        # codebox settings
        self.codebox_width = 700
        self.codebox_width_pixels = True
        self.codebox_height = 100
        # if to check for version for auto update
        self.check_version = False
        # reload settings
        self.reload_doc_last = True
        # backup settings
        self.backup_copy = True
        self.autosave_on_quit = False
        self.autosave = [False, 5]


        self.rt_show_white_spaces = False

        self.hpaned_pos = 170
        self.show_node_name_label = True
        self.nodes_icons = "c"
        self.recent_docs = []

        # setup spell checker
        self.spell_check_lang = SPELL_CHECK_LANG_DEFAULT
        self.enable_spell_check = False
        #debug info
        debug_stuff.debug_output("ConfigSettings object values\n" + str(self.__dict__), 'debug', 'config.py', 'configsettings __init__')  # print the attrib values

    def config_file_load(self):
        """Load the Preferences from Config File
        :param self: core.CherryTree object
        """
        if os.path.isfile(const.CONFIG_PATH):
            config = ConfigParser.RawConfigParser()
            config.read(const.CONFIG_PATH)

            section = "state"
            self.file_dir = unicode(config.get(section, "file_dir"), const.STR_UTF8, const.STR_IGNORE) if config.has_option(
                section, "file_dir") else ""
            self.file_name = unicode(config.get(section, "file_name"), const.STR_UTF8, const.STR_IGNORE) if config.has_option(
                section, "file_name") else ""
            self.toolbar_visible = config.getboolean(section, "toolbar_visible") if config.has_option(section,
                                                                                                      "toolbar_visible") else True
            self.win_is_maximized = config.getboolean(section, "win_is_maximized") if config.has_option(section,
                                                                                                        "win_is_maximized") else False
            # restore window size and position
            if config.has_option(section, "win_position_x") and config.has_option(section, "win_position_y"):
                self.win_position = [config.getint(section, "win_position_x"), config.getint(section, "win_position_y")]
            else:
                self.win_position = [10, 10]
            if config.has_option(section, "win_size_w") and config.has_option(section, "win_size_h"):
                self.win_size = [config.getint(section, "win_size_w"), config.getint(section, "win_size_h")]
            self.hpaned_pos = config.getint(section, "hpaned_pos") if config.has_option(section, "hpaned_pos") else 170

            if config.has_option(section, "node_path"):
                # restore the selected node
                self.node_path = gui.tree_functions.get_node_path_from_str(config.get(section, "node_path"))
                self.cursor_position = config.getint(section, "cursor_position") if config.has_option(section,
                                                                                                      "cursor_position") else 0
            else:
                self.node_path = None
            # inst.recent_docs = []  moved to core init
            if config.has_option(section, "recent_docs"):
                temp_recent_docs = config.get(section, "recent_docs").split(const.CHAR_SPACE)
                for element in temp_recent_docs:
                    if element:
                        self.recent_docs.append(base64.b64decode(element), const.STR_UTF8, const.STR_IGNORE)
            self.pick_dir = config.get(section, "pick_dir") if config.has_option(section, "pick_dir") else ""
            self.link_type = config.get(section, "link_type") if config.has_option(section,
                                                                                   "link_type") else const.LINK_TYPE_WEBS
            self.show_node_name_label = config.getboolean(section, "show_node_name_label") if config.has_option(section,
                                                                                                                "show_node_name_label") else True
            if config.has_option(section, "toolbar_icon_size"):
                self.toolbar_icon_size = config.getint(section, "toolbar_icon_size")
                if self.toolbar_icon_size not in ICONS_SIZE:
                    self.toolbar_icon_size = 1
            else:
                self.toolbar_icon_size = 1
            self.curr_colors = {'f': gtk.gdk.color_parse(config.get(section, "fg")) if config.has_option(section, "fg") else None,
                                'b': gtk.gdk.color_parse(config.get(section, "bg")) if config.has_option(section, "bg") else None}

            section = "tree"
            self.rest_exp_coll = config.getint(section, "rest_exp_coll") if config.has_option(section,
                                                                                              "rest_exp_coll") else 0
            self.expanded_collapsed_string = config.get(section, "expanded_collapsed_string") if config.has_option(section,
                                                                                                                   "expanded_collapsed_string") else ""
            self.expcollnam1 = unicode(config.get(section, "expcollnam1"), const.STR_UTF8,
                                       const.STR_IGNORE) if config.has_option(section, "expcollnam1") else ""
            self.expcollstr1 = config.get(section, "expcollstr1") if config.has_option(section, "expcollstr1") else ""
            self.expcollsel1 = config.get(section, "expcollsel1") if config.has_option(section, "expcollsel1") else ""
            self.expcollcur1 = config.getint(section, "expcollcur1") if config.has_option(section, "expcollcur1") else 0
            self.expcollnam2 = unicode(config.get(section, "expcollnam2"), const.STR_UTF8,
                                       const.STR_IGNORE) if config.has_option(section, "expcollnam2") else ""
            self.expcollstr2 = config.get(section, "expcollstr2") if config.has_option(section, "expcollstr2") else ""
            self.expcollsel2 = config.get(section, "expcollsel2") if config.has_option(section, "expcollsel2") else ""
            self.expcollcur2 = config.getint(section, "expcollcur2") if config.has_option(section, "expcollcur2") else 0
            self.expcollnam3 = unicode(config.get(section, "expcollnam3"), const.STR_UTF8,
                                       const.STR_IGNORE) if config.has_option(section, "expcollnam3") else ""
            self.expcollstr3 = config.get(section, "expcollstr3") if config.has_option(section, "expcollstr3") else ""
            self.expcollsel3 = config.get(section, "expcollsel3") if config.has_option(section, "expcollsel3") else ""
            self.expcollcur3 = config.getint(section, "expcollcur3") if config.has_option(section, "expcollcur3") else 0
            self.nodes_icons = config.get(section, "nodes_icons") if config.has_option(section, "nodes_icons") else "c"
            self.tree_right_side = config.getboolean(section, "tree_right_side") if config.has_option(section,
                                                                                                      "tree_right_side") else False
            self.cherry_wrap_width = config.getint(section, "cherry_wrap_width") if config.has_option(section,
                                                                                                      "cherry_wrap_width") else 130

            section = "editor"
            self.syntax_highlighting = config.get(section, "syntax_highlighting") if config.has_option(section,
                                                                                                       "syntax_highlighting") else const.RICH_TEXT_ID
            self.auto_syn_highl = config.get(section, "auto_syn_highl") if config.has_option(section,
                                                                                             "auto_syn_highl") else "sh"
            self.style_scheme = config.get(section, "style_scheme") if config.has_option(section,
                                                                                         "style_scheme") else const.STYLE_SCHEME_DEFAULT
            self.enable_spell_check = config.getboolean(section, "enable_spell_check") if config.has_option(section,
                                                                                                            "enable_spell_check") else False
            self.spell_check_lang = config.get(section, "spell_check_lang") if config.has_option(section,
                                                                                                 "spell_check_lang") else SPELL_CHECK_LANG_DEFAULT
            self.show_line_numbers = config.getboolean(section, "show_line_numbers") if config.has_option(section,
                                                                                                          "show_line_numbers") else False
            self.spaces_instead_tabs = config.getboolean(section, "spaces_instead_tabs") if config.has_option(section,
                                                                                                              "spaces_instead_tabs") else True
            self.tabs_width = config.getint(section, "tabs_width") if config.has_option(section, "tabs_width") else 4
            self.anchor_size = config.getint(section, "anchor_size") if config.has_option(section, "anchor_size") else 16
            self.embfile_size = config.getint(section, "embfile_size") if config.has_option(section, "embfile_size") else 48
            self.line_wrapping = config.getboolean(section, "line_wrapping") if config.has_option(section,
                                                                                                  "line_wrapping") else True
            self.wrapping_indent = config.getint(section, "wrapping_indent") if config.has_option(section,
                                                                                                  "wrapping_indent") else -14
            self.auto_indent = config.getboolean(section, "auto_indent") if config.has_option(section,
                                                                                              "auto_indent") else True
            self.rt_show_white_spaces = config.getboolean(section, "rt_show_white_spaces") if config.has_option(section,
                                                                                                                "rt_show_white_spaces") else False
            self.show_white_spaces = config.getboolean(section, "show_white_spaces") if config.has_option(section,
                                                                                                          "show_white_spaces") else True
            self.highl_curr_line = config.getboolean(section, "highl_curr_line") if config.has_option(section,
                                                                                                      "highl_curr_line") else True
            self.h_rule = config.get(section, "h_rule") if config.has_option(section, "h_rule") else const.HORIZONTAL_RULE
            self.special_chars = unicode(config.get(section, "special_chars") if config.has_option(section,
                                                                                                   "special_chars") else const.SPECIAL_CHARS_DEFAULT,
                                         const.STR_UTF8, const.STR_IGNORE)
            self.timestamp_format = config.get(section, "timestamp_format") if config.has_option(section,
                                                                                                 "timestamp_format") else "%Y/%m/%d - %H:%M"
            if config.has_option(section, "weblink_custom_action"):
                temp_str = config.get(section, "weblink_custom_action")
                self.weblink_custom_action = [True, temp_str[4:]] if temp_str[:4] == "True" else [False, temp_str[5:]]
            else:
                self.weblink_custom_action = [False, const.LINK_CUSTOM_ACTION_DEFAULT_WEB]
            if config.has_option(section, "filelink_custom_action"):
                temp_str = config.get(section, "filelink_custom_action")
                self.filelink_custom_action = [True, temp_str[4:]] if temp_str[:4] == "True" else [False, temp_str[5:]]
            else:
                self.filelink_custom_action = [False, const.LINK_CUSTOM_ACTION_DEFAULT_FILE]
            if config.has_option(section, "folderlink_custom_action"):
                temp_str = config.get(section, "folderlink_custom_action")
                self.folderlink_custom_action = [True, temp_str[4:]] if temp_str[:4] == "True" else [False, temp_str[5:]]
            else:
                self.folderlink_custom_action = [False, const.LINK_CUSTOM_ACTION_DEFAULT_FILE]

            section = "codebox"
            if config.has_option(section, "codebox_width"):
                self.codebox_width = config.getfloat(section, "codebox_width")
            else:
                self.codebox_width = 700
            if config.has_option(section, "codebox_height"):
                self.codebox_height = config.getfloat(section, "codebox_height")
            else:
                self.codebox_height = 100
            self.codebox_width_pixels = config.getboolean(section, "codebox_width_pixels") if config.has_option(section,
                                                                                                                "codebox_width_pixels") else True

            section = "table"
            self.table_rows = config.getint(section, "table_rows") if config.has_option(section, "table_rows") else 3
            self.table_columns = config.getint(section, "table_columns") if config.has_option(section,
                                                                                              "table_columns") else 3
            self.table_column_mode = config.get(section, "table_column_mode") if config.has_option(section,
                                                                                                   "table_column_mode") else "rename"
            self.table_col_min = config.getint(section, "table_col_min") if config.has_option(section,
                                                                                              "table_col_min") else 40
            self.table_col_max = config.getint(section, "table_col_max") if config.has_option(section,
                                                                                              "table_col_max") else 60

            section = "fonts"
            self.text_font = config.get(section, "text_font") if config.has_option(section,
                                                                                   "text_font") else "Sans 9"  # default text font
            self.tree_font = config.get(section, "tree_font") if config.has_option(section,
                                                                                   "tree_font") else "Sans 8"  # default tree font
            self.code_font = config.get(section, "code_font") if config.has_option(section,
                                                                                   "code_font") else "Monospace 9"  # default code font

            section = "colors"
            self.rt_def_fg = config.get(section, "rt_def_fg") if config.has_option(section,
                                                                                   "rt_def_fg") else const.RICH_TEXT_DARK_FG
            self.rt_def_bg = config.get(section, "rt_def_bg") if config.has_option(section,
                                                                                   "rt_def_bg") else const.RICH_TEXT_DARK_BG
            self.tt_def_fg = config.get(section, "tt_def_fg") if config.has_option(section,
                                                                                   "tt_def_fg") else const.TREE_TEXT_LIGHT_FG
            self.tt_def_bg = config.get(section, "tt_def_bg") if config.has_option(section,
                                                                                   "tt_def_bg") else const.TREE_TEXT_LIGHT_BG
            if config.has_option(section, "palette_list"):
                self.palette_list = config.get(section, "palette_list").split(":")
            else:
                self.palette_list = const. COLOR_PALETTE_DEFAULT
            self.col_link_webs = config.get(section, "col_link_webs") if config.has_option(section,
                                                                                           "col_link_webs") else const.COLOR_48_LINK_WEBS
            self.col_link_node = config.get(section, "col_link_node") if config.has_option(section,
                                                                                           "col_link_node") else const.COLOR_48_LINK_NODE
            self.col_link_file = config.get(section, "col_link_file") if config.has_option(section,
                                                                                           "col_link_file") else const.COLOR_48_LINK_FILE
            self.col_link_fold = config.get(section, "col_link_fold") if config.has_option(section,
                                                                                           "col_link_fold") else const.COLOR_48_LINK_FOLD

            section = "misc"
            self.systray = config.getboolean(section, "systray") if config.has_option(section, "systray") else False
            self.start_on_systray = config.getboolean(section, "start_on_systray") if config.has_option(section,
                                                                                                        "start_on_systray") else False
            self.use_appind = config.getboolean(section, "use_appind") if config.has_option(section,
                                                                                            "use_appind") else False
            if config.has_option(section, "autosave") and config.has_option(section, "autosave_val"):
                self.autosave = [config.getboolean(section, "autosave"), config.getint(section, "autosave_val")]
            else:
                self.autosave = [False, 5]
            self.check_version = config.getboolean(section, "check_version") if config.has_option(section,
                                                                                                  "check_version") else False
            self.reload_doc_last = config.getboolean(section, "reload_doc_last") if config.has_option(section,
                                                                                                      "reload_doc_last") else True
            self.enable_mod_time_sentinel = config.getboolean(section, "mod_time_sent") if config.has_option(section,
                                                                                                             "mod_time_sent") else True
            self.backup_copy = config.getboolean(section, "backup_copy") if config.has_option(section,
                                                                                              "backup_copy") else True
            self.autosave_on_quit = config.getboolean(section, "autosave_on_quit") if config.has_option(section,
                                                                                                        "autosave_on_quit") else False
            self.limit_undoable_steps = config.getint(section, "limit_undoable_steps") if config.has_option(section,
                                                                                                            "limit_undoable_steps") else 20
            debug_stuff.debug_output("read %s (%s, %s)" % (const.CONFIG_PATH, self.file_name, self.file_dir), 'debug', 'config.py', 'config_load_file()')
        else:
            debug_stuff.debug_output("missing %s" % (const.CONFIG_PATH), 'error', 'config.py', 'config_load_file()')

    def config_file_save(self, cherrytreeobject):
        """Save the Preferences to Config File
        :param cherrytreeobject:  pass the main cherrytree object so the settings can access the window settings
        :param self: defualt for functions in a class
        """
        config = ConfigParser.RawConfigParser()
        # state settings
        section = "state"
        config.add_section(section)
        config.set(section, "file_dir", self.file_dir)  # file directory
        config.set(section, "file_name", self.file_name)  # filename
        config.set(section, "toolbar_visible", cherrytreeobject.window.ui.get_widget("/ToolBar").get_property(const.STR_VISIBLE))
        config.set(section, "win_is_maximized", self.win_is_maximized)
        self.win_position = cherrytreeobject.window.get_position()
        config.set(section, "win_position_x", self.win_position[0])
        config.set(section, "win_position_y", self.win_position[1])
        if not self.win_is_maximized:
            win_size = cherrytreeobject.window.get_size()
            config.set(section, "win_size_w", win_size[0])
            config.set(section, "win_size_h", win_size[1])
        config.set(section, "hpaned_pos", cherrytreeobject.window.hpaned.get_property('position'))
        if cherrytreeobject.curr_tree_iter:
            config.set(section, "node_path", gui.tree_functions.get_node_path_str_from_path(cherrytreeobject.window.treestore.get_path(cherrytreeobject.curr_tree_iter)))
            config.set(section, "cursor_position", cherrytreeobject.curr_buffer.get_property(const.STR_CURSOR_POSITION))
        if cherrytreeobject.configsettingsobject.recent_docs:
            temp_recent_docs = []
            for i, element in enumerate(cherrytreeobject.configsettingsobject.recent_docs):
                if i >= const.MAX_RECENT_DOCS:
                    break
                temp_recent_docs.append(base64.b64encode(element))
            str_recent_docs = const.CHAR_SPACE.join(temp_recent_docs)
        else:
            str_recent_docs = ""
        config.set(section, "recent_docs", str_recent_docs)
        config.set(section, "pick_dir", self.pick_dir)
        config.set(section, "link_type", self.link_type)
        config.set(section, "show_node_name_label", cherrytreeobject.window.header_node_name_label.get_property(const.STR_VISIBLE))
        config.set(section, "toolbar_icon_size", self.toolbar_icon_size)
        if self.curr_colors['f']:
            config.set(section, "fg", self.curr_colors['f'].to_string())
        if self.curr_colors['b']:
            config.set(section, "bg", self.curr_colors['b'].to_string())

        section = "tree"
        config.add_section(section)
        config.set(section, "rest_exp_coll", self.rest_exp_coll)
        if self.rest_exp_coll == 0:
            gui.tree_functions.get_tree_expanded_collapsed_string(self.parent)
            config.set(section, "expanded_collapsed_string", self.expanded_collapsed_string)
        if self.expcollnam1 and self.expcollnam1 != self.file_name:
            config.set(section, "expcollnam1", self.expcollnam1)
            config.set(section, "expcollstr1", self.expcollstr1)
            config.set(section, "expcollsel1", self.expcollsel1)
            config.set(section, "expcollcur1", self.expcollcur1)
        if self.expcollnam2 and self.expcollnam2 != self.file_name:
            config.set(section, "expcollnam2", self.expcollnam2)
            config.set(section, "expcollstr2", self.expcollstr2)
            config.set(section, "expcollsel2", self.expcollsel2)
            config.set(section, "expcollcur2", self.expcollcur2)
        if self.expcollnam3 and self.expcollnam3 != self.file_name:
            config.set(section, "expcollnam3", self.expcollnam3)
            config.set(section, "expcollstr3", self.expcollstr3)
            config.set(section, "expcollsel3", self.expcollsel3)
            config.set(section, "expcollcur3", self.expcollcur3)
        config.set(section, "nodes_icons", self.nodes_icons)
        config.set(section, "tree_right_side", self.tree_right_side)
        config.set(section, "cherry_wrap_width", self.cherry_wrap_width)

        section = "editor"
        config.add_section(section)
        config.set(section, "syntax_highlighting", self.syntax_highlighting)
        config.set(section, "auto_syn_highl", self.auto_syn_highl)
        config.set(section, "style_scheme", self.style_scheme)
        config.set(section, "spell_check_lang", self.spell_check_lang)
        config.set(section, "enable_spell_check", self.enable_spell_check)
        config.set(section, "show_line_numbers", self.show_line_numbers)
        config.set(section, "spaces_instead_tabs", self.spaces_instead_tabs)
        config.set(section, "tabs_width", self.tabs_width)
        config.set(section, "anchor_size", self.anchor_size)
        config.set(section, "embfile_size", self.embfile_size)
        config.set(section, "line_wrapping", self.line_wrapping)
        config.set(section, "wrapping_indent", self.wrapping_indent)
        config.set(section, "auto_indent", self.auto_indent)
        config.set(section, "rt_show_white_spaces", self.rt_show_white_spaces)
        config.set(section, "show_white_spaces", self.show_white_spaces)
        config.set(section, "highl_curr_line", self.highl_curr_line)
        config.set(section, "h_rule", self.h_rule)
        config.set(section, "special_chars", self.special_chars)
        config.set(section, "timestamp_format", self.timestamp_format)
        config.set(section, "weblink_custom_action", str(self.weblink_custom_action[0]) + self.weblink_custom_action[1])
        config.set(section, "filelink_custom_action", str(self.filelink_custom_action[0]) + self.filelink_custom_action[1])
        config.set(section, "folderlink_custom_action",
                   str(self.folderlink_custom_action[0]) + self.folderlink_custom_action[1])

        # codeebox settings
        section = "codebox"
        config.add_section(section)
        config.set(section, "codebox_width", self.codebox_width)
        config.set(section, "codebox_height", self.codebox_height)
        config.set(section, "codebox_width_pixels", self.codebox_width_pixels)

        # table settings
        section = "table"
        config.add_section(section)
        config.set(section, "table_rows", self.table_rows)
        config.set(section, "table_columns", self.table_columns)
        config.set(section, "table_column_mode", self.table_column_mode)
        config.set(section, "table_col_min", self.table_col_min)
        config.set(section, "table_col_max", self.table_col_max)

        # fonts settings
        section = "fonts"
        config.add_section(section)
        config.set(section, "text_font", self.text_font)
        config.set(section, "tree_font", self.tree_font)
        config.set(section, "code_font", self.code_font)

        # colours settings
        section = "colors"
        config.add_section(section)
        config.set(section, "rt_def_fg", self.rt_def_fg)
        config.set(section, "rt_def_bg", self.rt_def_bg)
        config.set(section, "tt_def_fg", self.tt_def_fg)
        config.set(section, "tt_def_bg", self.tt_def_bg)
        config.set(section, "palette_list", ":".join(self.palette_list))
        config.set(section, "col_link_webs", self.col_link_webs)
        config.set(section, "col_link_node", self.col_link_node)
        config.set(section, "col_link_file", self.col_link_file)
        config.set(section, "col_link_fold", self.col_link_fold)

        #misc settings
        section = "misc"
        config.add_section(section)
        config.set(section, "systray", self.systray)
        config.set(section, "start_on_systray", self.start_on_systray)
        config.set(section, "use_appind", self.use_appind)
        config.set(section, "autosave", self.autosave[0])
        config.set(section, "autosave_val", self.autosave[1])
        config.set(section, "check_version", self.check_version)
        config.set(section, "reload_doc_last", self.reload_doc_last)
        config.set(section, "mod_time_sent", self.enable_mod_time_sentinel)
        config.set(section, "backup_copy", self.backup_copy)
        config.set(section, "autosave_on_quit", self.autosave_on_quit)
        config.set(section, "limit_undoable_steps", self.limit_undoable_steps)

        #write the settings to the file system
        with open(const.CONFIG_PATH, 'wb') as configfile:
            config.write(configfile)
            debug_stuff.debug_output("Output Path %s" % (const.CONFIG_PATH), 'debug', 'config.py', 'config_file_save()')
            debug_stuff.debug_output("Filename %s   file path %s " % (self.file_name, self.file_dir), 'debug', 'config.py', 'config_file_save()')



